package game.graphics;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;

import datastructures.Coordinate;
import datastructures.Word;

/**
 * 
 * @author Fredrik
 *
 */
public class GWord {

	private static final Color TAKEN_COLOR = Color.BLUE;
	private static final Color GUESSING_COLOR = Color.GRAY;
	private static final Color MIDDLE_COLOR = Color.BLACK;

	/** The marginal between the word and the edge */
	public static final int WORD_MARGINAL = 5;

	private Word word;

	private boolean editingMode = false;
	private int stringWidth = 0;
	private int stringHeight = 0;
	private int dotWidth = 0;
	private FontRenderContext myFontRenderContext = new FontRenderContext(
			new AffineTransform(), true, true);
	private Font f;

	/**
	 * Creating a graphic word
	 * @param w
	 */
	public GWord(Word w) {
		this.word = w;
		this.f = new Font(Font.MONOSPACED, Font.BOLD, 12);

		Rectangle2D rec = this.f.getStringBounds(this.word.getWordText(),
				this.myFontRenderContext);
		this.stringWidth = rec.getBounds().width;
		this.stringHeight = rec.getBounds().height;
		rec = this.f.getStringBounds(".",
				this.myFontRenderContext);
		this.dotWidth = rec.getBounds().width;
	}

	/**
	 * Paints this word on the graphics g
	 * @param g
	 */
	public void paint(Graphics g) {

		if (this.word.isTaken() || this.editingMode || this.word.isMiddle()) {
			g.setColor(Color.ORANGE);
			g.fillRect(getX(), getY(), getWidth(), getHeight());
			g.setColor(getColor());
			g.drawRect(getX(), getY(), getWidth(), getHeight());
			g.setFont(this.f);
			g.drawString(this.word.getWordText(), getX() + WORD_MARGINAL, getY()
					+ (getHeight() / 2) + this.stringHeight / 4);
		}else if(this.word.isVisible()){
			g.setColor(Color.ORANGE);
			g.fillRect(getX(), getY(), getWidth(), getHeight());
			g.setColor(getColor());
			g.drawRect(getX(), getY(), getWidth(), getHeight());
			g.setFont(this.f);
			g.drawString(drawDots(this.word.getWordText()), getDotX(), getY()
					+ (getHeight() / 2) + this.stringHeight / 4);
		}

	}

	/**
	 * Returns true if the coordinates is in the words boundary
	 * @param c
	 * @return true if the coordinates is in the words boundary
	 */
	public boolean inBoundaryBox(Coordinate c) {

		return (c.getX() >= getX() && c.getX() <= (getX() + getWidth())
				&& c.getY() >= getY() && c.getY() <= (getY() + getHeight()));
	}

	/**
	 * Returns the word used by this graphic word
	 * @return the word used by this graphic word
	 */
	public Word getWord() {
		return this.word;
	}

	private Color getColor() {
		if (this.word.isMiddle()) {
			return MIDDLE_COLOR;
		} else if (this.word.isTaken()) {
			return TAKEN_COLOR;
		} else {
			return GUESSING_COLOR;
		}
	}

	private int getX() {
		return this.word.getCordinates().getX() - (getWidth() / 2);
	}

	private int getY() {
		return this.word.getCordinates().getY() - (getHeight() / 2);
	}

	private int getWidth() {
		return (this.stringWidth + 2 * WORD_MARGINAL);
	}

	private int getHeight() {
		return (this.stringHeight + 2 * WORD_MARGINAL);
	}
	
	private String drawDots(String word){
		String retVal = "";
		for(int i = 0; i< word.length();i++){
			char c = word.charAt(i);
			if(c != ' ' && c != '-'){
				retVal += ".";
			}else{
				retVal += c;
			}
		} 
		return retVal;
	}
	
	private int getDotX(){
		return getX() + getWidth()/2 - (this.dotWidth*this.word.getWordText().length())/2 + WORD_MARGINAL;
	}

	/**
	 * returns true if this word is in editing mode
	 * @return true if this word is in editing mode
	 */
	public boolean isEditingMode() {
		return this.editingMode;
	}

	/**
	 * Set to true if you want to be in editing mode and want this word to be visible
	 * @param editingMode
	 */
	public void setEditingMode(boolean editingMode) {
		this.editingMode = editingMode;
	}

}
